package controller.employee;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;

import model.Employee;
import view.Home;
import view.dialog.EmployeeChangePasswordDialog;
import dao.EmployeeDAO;


public class ChangePasswordController implements ActionListener, Observer	{

	private EmployeeChangePasswordDialog emp;
	private EmployeeDAO empDAO;
	private Employee employee;
	
	
	public ChangePasswordController(EmployeeChangePasswordDialog emp) {
		super();
		this.emp = emp;
		employee = Home.getEmployee();
		empDAO = new EmployeeDAO();
	}


	public void setInfo()
	{
		employee = Home.getEmployee();
		emp.getTxt_emdlg_name().setText(employee.getName());
		emp.getTxt_emdlg_email().setText(employee.getEmail());
		emp.getTxt_emdlg_personalID().setText(employee.getEmpPerId());
		emp.getTxta_emdlg_address().setText(employee.getAddress());
		emp.getTxt_emdlg_phone().setText(employee.getPhone());
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		String empName = emp.getTxt_emdlg_name().getText();
		employee.setName(empName);

		String empID = emp.getTxt_emdlg_personalID().getText();
		employee.setEmpPerId(empID);;

		String empEmail = emp.getTxt_emdlg_email().getText();
		employee.setEmail(empEmail);

		String empPhone = emp.getTxt_emdlg_phone().getText();
		employee.setPhone(empPhone);

		String empAdd = emp.getTxta_emdlg_address().getText();
		employee.setAddress(empAdd);

		String empNewPass = new String(emp.getTxt_emdlg_newpassword().getPassword());
		employee.setPassword(empNewPass);
		
		
		if (empDAO.updateEmployee(employee)) {
			Home.getLblMainUser().setText("Welcome: "+employee.getName());
			JOptionPane.showMessageDialog(null, "Update successfully");
		}
	}


	@Override
	public void update(Observable o, Object arg) {
		setInfo();
	}
}
